/*
 * UIEntity.fx
 *
 * Created on Feb 15, 2010, 8:42:47 PM
 */

package dawnland03.ui.model;

import javafx.scene.*;
import javafx.scene.paint.Color;
import javafx.scene.shape.*;
import javafx.scene.transform.*;
import javafx.util.Math;

import dawnland03.ui.service.UIService;

/**
 * @author user
 */

public class UIEntity extends CustomNode{
    public var uiHexagon: UIHexagon on replace {
        centerX = uiHexagon.getCenterXToParent();
        centerY = uiHexagon.getCenterYToParent();
        };
    public var uiService : UIService;

    public var entityId : Integer;
    public var centerX : Number;
    public var centerY : Number;
    package var rotateAngle = 0.0;

    var group : Group = Group {};

    init {
        var backgroundPolygon  = Polygon{
            points: [6, 0, 8, 6, 10, 8, 12, 12, 6, 10, 0, 12, 2, 8, 4, 6];
            fill: Color.YELLOW;
        }
        var badgeArrow = Polygon{
            points: [6, 4.5, 8, 6.5, 8, 8, 6, 6, 4, 8, 4, 6.5];
            fill: Color.BLUE;
        }
        var badgeLine = Polygon{
            points: [8, 8.5, 8, 10, 4, 10, 4, 8.5];
            fill: Color.BLUE;
        }
        var figure = Group {
            content: [backgroundPolygon, badgeArrow, badgeLine]
            onMousePressed: function(event) {
                  if(event.primaryButtonDown){
                      println("id selected: {entityId}");
                      uiService.setSelectedUIEntity(entityId);
                  }
            }
        }

        def width = figure.boundsInLocal.maxX-figure.boundsInLocal.minX;
        def height = figure.boundsInLocal.maxY-figure.boundsInLocal.minY;
        def figureScaleFactor = Math.min(2* uiService.getYStep() / width, 2* uiService.getYStep() / height);
        figure.transforms = [Translate {
                x: bind centerX - width * figureScaleFactor * 0.5,
                y: bind centerY - height * figureScaleFactor * 0.5},
            Scale {
                x: figureScaleFactor;
                y: figureScaleFactor;},
            Rotate {
                angle: bind rotateAngle;
                pivotX: width * 0.5;
                pivotY: height * 0.5;
                }];
        insert figure into group.content;
    }

    override function create(): Node {
        return group;
    }

}
